package com.wujunshen.algorithm.leetcode.basic;

/**
 * @author frank woo(吴峻申) <br>
 *     email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2022/7/25 01:37<br>
 */
public class 山脉数组的峰顶索引 {
  public int peakIndexInMountainArray(int[] arr) {
    if (arr.length == 0) {
      return -1;
    }

    int start = 0;
    int end = arr.length - 1;
    int target = 0;

    while (start < end) {
      int mid = start + (end - start) / 2;
      // Comparing (mid) with (mid+1)
      target = arr[mid + 1];

      if (target < arr[mid]) {
        end = mid;
      } else if (target > arr[mid]) {
        start = mid + 1;
      }
    }
    // In the end start = end, so you can return either start or end
    return end;
  }
}
